/* ------------------------------------------------------------------
 *   Product:      mybatis
 *   Module Name:  learn
 *   Package Name: learn
 *   Date Created: 2018-09-16
 * ------------------------------------------------------------------
 * Modification History
 * DATE            Name           Contact
 * ------------------------------------------------------------------
 * 2018-09-16      Jie            GloryJie@163.com
 */
package com.gloryjie.learn;

import com.gloryjie.learn.dao.UserMapper;
import com.gloryjie.learn.model.User;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 * @author Jie
 * @since
 */
public class App {

  public static void main(String[] args) throws IOException {
    SqlSession sqlSession = null;
    User user1 = null;
    InputStream inputStream = Resources
        .getResourceAsStream("com/gloryjie/learn/mybatis-config.xml");
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
    sqlSession = factory.openSession();

    try {
      UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

      user1 = userMapper.getUserByName("jie");

      user1.setName("rong123");

      User user2 = userMapper.getUserByName("jie");
      System.out.println(user2.toString());
      System.out.println(user1 == user2);

    } finally {
      if (sqlSession != null) {
        sqlSession.close();
      }
    }

    try {
      sqlSession = factory.openSession();
      UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

      User user2 = userMapper.getUserByName("jie");
      System.out.println(user2.toString());
      System.out.println(user1 == user2);

    } finally {
      if (sqlSession != null) {
        sqlSession.close();
      }
    }
  }

}
